package com.usian.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.behavior.mapper.ApBehaviorEntryMapper;
import com.usian.behavior.mapper.ApLikesBehaviorMapper;
import com.usian.behavior.service.ApLikesBehaviorService;
import com.usian.model.behavior.dtos.LikesBehaviorDto;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApLikesBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior>implements ApLikesBehaviorService {

    @Autowired
    private ApLikesBehaviorMapper apLikesBehaviorMapper;

    @Autowired
    private ApBehaviorEntryMapper apBehaviorEntryMapper;


    @Override/*保存 点赞行为*/
    public ResponseResult like(LikesBehaviorDto dto) {

        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        /*获取用户的id  就是为了查询实体 因为实体的字段entry_id 就是 用户id*/
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"登录用户不存在");
        }

        /*查询实体*/
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryMapper.findApBehaviorEntry(user.getId(), null);

        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"实体不存在");
        }

        /*避免重复保存*/
        List<ApLikesBehavior> list = apLikesBehaviorMapper.selectList(Wrappers.<ApLikesBehavior>lambdaQuery().eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId())
                .eq(ApLikesBehavior::getArticleId, dto.getArticleId()).eq(ApLikesBehavior::getType, dto.getType()));

        /*如果为0则添加  如果查到了就修改*/
        ApLikesBehavior apLikesBehavior = new ApLikesBehavior();
        if (list.size()==0) {
            if (dto.getOperation()==0) {
                apLikesBehavior.setEntryId(apBehaviorEntry.getId());
                apLikesBehavior.setOperation(dto.getOperation());
                apLikesBehavior.setArticleId(dto.getArticleId());
                apLikesBehavior.setType(dto.getType());
                apLikesBehavior.setCreatedTime(new Date());
                apLikesBehaviorMapper.saveApLikesBehavior(apLikesBehavior);
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
        }else if (list.size()==1){
            ApLikesBehavior LikesBehavior = list.get(0);
            LikesBehavior.setOperation(dto.getOperation());
            this.updateById(LikesBehavior);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
